package main

import (
	"test.com/src/protocol"
)

type Room struct {
	playerMgr PlayerMgr
	id        int
	msgs      Queue
}

func (r *Room) Init(id int) {
	r.playerMgr = *NewPlayerMgr()
	r.id = id
}

func (r *Room) PlayerIn(player *Player) []string {
	r.playerMgr.AddPlayer(player)
	player.RoomID = r.id
	return r.msgs.All()
}

func (r *Room) PlayerOut(name string) {
	r.playerMgr.RemovePlayer(name)
}

func (r *Room) handleChat(sender *Player, msg string) {
	//filter

	data, _ := protocol.BuildChatMsg(sender.Name, msg)
	if r.msgs.Size() >= 50 {
		r.msgs.Dequeue()
	}
	r.msgs.Enqueue(sender.Name + " 说: " + msg)

	for _, v := range *r.playerMgr.GetPlayers() {

		v.Session.AsyncWrite(data)
	}
}
